@file:Suppress("unused")

package com.example.demo.model.mall

import xyz.erupt.annotation.*
import xyz.erupt.annotation.sub_erupt.*
import xyz.erupt.annotation.sub_field.*
import xyz.erupt.annotation.sub_field.sub_edit.*
import xyz.erupt.upms.model.base.*
import javax.persistence.*

@Erupt(name = "商品管理", linkTree = LinkTree(field = "category"))
@Table(name = "mall_goods")
@Entity
class Goods : HyperModel() {

    @EruptField(
        views = [View(title = "商品图片")],
        edit = Edit(
            title = "商品图片",
            notNull = true,
            type = EditType.ATTACHMENT,
            attachmentType = AttachmentType(type = AttachmentType.Type.IMAGE, maxLimit = 6),
        ),
    )
    var image: String = ""

    @EruptField(
        views = [View(title = "商品名称")],
        edit = Edit(
            title = "商品名称",
            notNull = true,
            inputType = InputType(fullSpan = true),
            search = Search(vague = true),
        ),
    )
    var name: String = ""

    @ManyToOne
    @EruptField(
        views = [View(title = "所属分类", column = "name")],
        edit = Edit(
            title = "所属分类",
            type = EditType.REFERENCE_TREE,
            search = Search(),
            notNull = true,
            referenceTreeType = ReferenceTreeType(pid = "parent.id"),
        ),
    )
    var category: GoodsCategory = GoodsCategory()

    @EruptField(
        views = [View(title = "运费")],
        edit = Edit(
            title = "运费",
            notNull = true,
            search = Search(vague = true),
        ),
    )
    var freight: Double = 0.0

    @EruptField(
        views = [View(title = "商品状态")],
        edit = Edit(
            title = "商品状态",
            notNull = true,
            boolType = BoolType(trueText = "上架", falseText = "下架"),
            search = Search(),
        ),
    )
    var status: Boolean = false

    @Lob
    @EruptField(
        views = [View(title = "商品描述", type = ViewType.HTML)],
        edit = Edit(title = "商品描述", type = EditType.HTML_EDITOR),
    )
    var description: String? = null

    @OneToMany(cascade = [CascadeType.ALL], orphanRemoval = true)
    @JoinColumn(name = "goods_id")
    @EruptField(edit = Edit(title = "商品型号", type = EditType.TAB_TABLE_ADD))
    var goodsSpecs: Set<GoodsSpec>? = null

}